'''
导入K12 资源到mongodb 数据库
把这个文件放在K12文件夹外面执行
jhwang
'''
import os
import time
from datetime import datetime
import logging
import pymongo
from gridfs import GridFS

LOG_FILENAME = 'k12.log'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)

def printMsg(msg):
	try:
		print(msg)
	except Exception as e:
		print(e)
	pass

if __name__ == '__main__':
	startIndex=89600

	logging.debug('Start import K12 data:'+datetime.now().strftime("%A, %d. %B %Y %I:%M%p"))
	connection=pymongo.Connection("localhost",27017)
	db=connection.test
	fs=GridFS(db)

	curDir=os.getcwd()
	root=os.path.join(curDir,"K12")
	count=1;
	for parent,dirnames,filenames in os.walk(root):
		for f in filenames:
			fullpath=os.path.join(parent,f)
			fileId=fullpath.replace(root,'')
			contentType=os.path.splitext(fullpath)[1].lstrip('.')
			print('\r\n--------正在处理第 {0} 个文件----------------'.format(count))		
			if count>startIndex:
				if not fs.exists(filename=fileId):
					try:
						with open(fullpath,'rb') as myfile:
							oid=fs.put(myfile,content_type=contentType,filename=fileId)
						printMsg(fileId+':导入成功!')
						time.sleep(0.03)
					except Exception as e:
						msg=fullpath+' 导入失败!!'
						logging.debug(msg)
						printMsg(msg+e)
				else:
					printMsg(fileId+' 已经存在!')	
			else:
				printMsg(fileId+'已处理!')
			count=count+1
	print('所有文件导入完毕!'+datetime.now().strftime("%A, %d. %B %Y %I:%M%p"))
	logging.debug('All done success!')
